package com.twm.serializer;

import com.twm.entity.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

public class RpcDecoder extends MessageToMessageDecoder<ByteBuf> {
    private final Class<?> clazz;

    private final Serializer serializer;

    public RpcDecoder(Class<?> clazz, Serializer serializer) {
        this.clazz = clazz;
        this.serializer = serializer;
    }

    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf msg, List<Object> out) throws Exception {
        byte[] req = new byte[msg.readableBytes()];
        msg.readBytes(req);
        out.add(serializer.deserialize(clazz, req));
    }
}
